# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=helix
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=25.01.1
pkgrel=1
pkgdesc="A post-modern modal text editor (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/helix-editor/helix"
license=('spdx:MPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-mdbook"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
optdepends=("${MINGW_PACKAGE_PREFIX}-rust-analyzer: Rust LSP"
            "${MINGW_PACKAGE_PREFIX}-clang-tools-extra: for clangd"
            "${MINGW_PACKAGE_PREFIX}-lldb: for lldb-dap")
source=("git+${url}.git#tag=${pkgver}"
        'icon.patch'
        'fix-runtime-dir.patch')
sha256sums=('b025a942bcab1f5818f762df22fd244643fe042cf2e51eab75303b53900f3bc0'
            'f45f0656cb1ceb5c90925c80b093335622f9ad3f64d4e2c8a3a0ea0ab43d900e'
            'f071538f25e12ca0898c61daa3a2fbf9416a5be4f014682942dcf34dfb0f1335')

prepare() {
  cd "${_realname}"

  if test true != "$(git config core.symlinks)"; then
    git config core.symlinks true &&
    MSYS='winsymlinks:nativestrict' git restore --source=HEAD :/
  fi

  patch -Np1 -i ../icon.patch
  patch -Np1 -i ../fix-runtime-dir.patch

  rm rust-toolchain.toml
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  cargo build --frozen --profile opt

  cd book

  mdbook build
}

check() {
  cd "${_realname}"

  cargo test --frozen --profile opt
}

package() {
  cd "${_realname}"

  install -Dm755 "target/opt/hx.exe" "${pkgdir}${MINGW_PREFIX}/bin/hx.exe"

  local runtime_dir="${pkgdir}${MINGW_PREFIX}/lib/${_realname}/runtime"
  mkdir -p "$runtime_dir/grammars"
  cp -r "runtime/queries" "$runtime_dir"
  cp -r "runtime/themes" "$runtime_dir"
  find "runtime/grammars" -type f -name '*.dll' -exec \
    install -Dm 755 {} -t "$runtime_dir/grammars" \;
  install -Dm644 runtime/tutor -t "$runtime_dir"

  install -Dm644 "contrib/completion/hx.bash" "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/hx"
  install -Dm644 "contrib/completion/hx.fish" "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/hx.fish"
  install -Dm644 "contrib/completion/hx.zsh"  "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_hx"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "book/book" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"

  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
